package com.dianzi.certification.controller;

import com.dianzi.certification.dto.CorrectImproveDTO;
import com.dianzi.certification.service.CorrectImproveService;
import com.dianzi.certification.utils.JwtUtils;
import com.dianzi.certification.vo.CorrectImproveAdminVO;
import com.dianzi.certification.vo.CorrectImproveVO;
import com.dianzi.certification.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author copa
 * @createDate 2022-03-30 22:17
 * @function
 */
@RestController
@Api(tags = {"流程：整改提高"})
public class CorrectImproveController {

    @Resource
    private CorrectImproveService correctImproveService;

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("improve")
    @ApiOperation(value = "查看整改提高(包括详情也调用这个接口)")
    public R<CorrectImproveVO> getImprove(HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return correctImproveService.getImprove(userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PostMapping("improve")
    @ApiOperation(value = "查看整改提高")
    public R<String> addImprove(@RequestBody CorrectImproveDTO correctImproveDTO,
                                HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return correctImproveService.addImprove(correctImproveDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PutMapping("improve")
    @ApiOperation(value = "修改整改提高")
    public R<String> putImprove(@RequestBody CorrectImproveDTO correctImproveDTO,
                                HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return correctImproveService.putImprove(correctImproveDTO, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @GetMapping("admin/improve/{certId}")
    @ApiOperation(value = "查看整改提高(认证专家)")
    public R<CorrectImproveAdminVO> getImproveDetailAdmin(@PathVariable Integer certId) {
        return correctImproveService.getImproveDetailAdmin(certId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @PutMapping("admin/improve")
    @ApiOperation(value = "判定认证是否通过：第五阶段")
    public R<String> putImproveAdmin(@RequestParam @ApiParam("认证id") Integer certId,
                                 @RequestParam @ApiParam("同意-1 驳回-2") Integer status,
                                 @RequestParam(required = false) @ApiParam("若驳回写理由") String remark) {
        return correctImproveService.putImproveAdmin(certId, status, remark);
    }

}
